package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.domain.TbUserComment;
import com.item.domain.TbUserCommentInit;
import com.item.dto.CommentDto;
import com.item.mapper.TbUserCommentInitMapper;
import com.item.mapper.TbUserCommentMapper;
import com.item.po.TbUserCommentInitPo;
import com.item.service.TbUserCommentInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TbUserCommentInitServiceImpl implements TbUserCommentInitService {

    @Autowired
    TbUserCommentInitMapper tbUserCommentInitMapper;

    @Autowired
    TbUserCommentMapper tbUserCommentMapper;

    public List<TbUserCommentInit> findAll(int limit, int page, TbUserCommentInit tbUserCommentInit) {
        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        return tbUserCommentInitMapper.findAll(tbUserCommentInit);
    }


    public int add(TbUserCommentInit tbUserCommentInit) {
        try {
            Thread.sleep(1000);
            return tbUserCommentInitMapper.insertSelective(tbUserCommentInit);
        } catch (InterruptedException e) {
            return 0;
        }
    }


    public int update(TbUserCommentInit tbUserCommentInit) {
        return tbUserCommentInitMapper.updateByPrimaryKey(tbUserCommentInit);
    }

    @Override
    public int delete(int id) {
        return tbUserCommentInitMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int statusAdd(CommentDto commentDto) {
        Integer commentId = commentDto.getCommentId();
        //传入帖子id跟类型,查询,如果有就删除,如果没有就新增
        TbUserCommentInit tbUserCommentInit1 = tbUserCommentInitMapper.findStatus(commentDto);
        if (tbUserCommentInit1 != null) {
            TbUserComment tb1 = new TbUserComment();

            Integer likeCount = commentDto.getLikeCount();
            Integer startCount = commentDto.getStartCount();
            //if(likeCount != 0) {
            try {
                Thread.sleep(100);
                if (commentDto.getStartType() == 3) {
                    tb1.setLikeCount((likeCount - 1));
                    System.out.println("取消喜欢");

                } else {
                    tb1.setStartCount((startCount) - 1);
                    System.out.println("取消点赞");
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //}
            tbUserCommentInitMapper.deleteByPrimaryKey(tbUserCommentInit1.getId());
            tb1.setId(commentId);
            tbUserCommentMapper.updateByPrimaryKeySelective(tb1);

            return 0;
        } else {
            TbUserCommentInit tbUserCommentInit = new TbUserCommentInit();
            TbUserComment tb2 = new TbUserComment();

            Integer likeCount = commentDto.getLikeCount();
            Integer startCount = commentDto.getStartCount();
            //if(likeCount != 0) {
            try {
                Thread.sleep(100);
                if (commentDto.getStartType() == 3) {
                    tb2.setLikeCount((likeCount + 1));
                    System.out.println("喜欢");
                } else {
                    tb2.setStartCount((startCount + 1));
                    System.out.println("点赞");
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tbUserCommentInit.setCreateTime(System.currentTimeMillis() + "");
            tbUserCommentInit.setStartType(commentDto.getStartType());
            tbUserCommentInit.setSendUserId(commentDto.getSendUserId());
            tbUserCommentInit.setCommentId(commentDto.getCommentId());
            tbUserCommentInitMapper.insertSelective(tbUserCommentInit);
            tb2.setId(commentDto.getCommentId());
            tbUserCommentMapper.updateByPrimaryKeySelective(tb2);

            return 1;
        }
    }

    @Override
    public List<TbUserCommentInitPo> tbUserCommentInitGet(int id) {
        Map<Integer, Integer> map = new HashMap<>();
        // select * from tb_user_comment_init t where t.comment_id = 1
        List<TbUserCommentInitPo> tbList = tbUserCommentInitMapper.tbUserCommentInitGet(id);

        //根据id查parentIdcount
        List<Integer> commentCount = new ArrayList<>();
        for (TbUserCommentInitPo tbUserCommentInitPo : tbList) {
            commentCount.add(tbUserCommentInitPo.getId());
        }

        for (Integer integer : commentCount) {
            Integer cId = tbUserCommentInitMapper.commentCount(integer);
            try {
                if (cId == null) {
                    map.put(integer, 0);
                } else {
                    map.put(integer, cId);
                }
            } catch (Exception e) {
                map.put(integer, 0);
            }

        }
        List<TbUserCommentInitPo> tbUserCommentInitPoList = new ArrayList<>();
        for (TbUserCommentInitPo tbUserCommentInitPo : tbList) {
            TbUserCommentInitPo tbUserCommentInitPo1 = tbUserCommentInitPo;
            tbUserCommentInitPo1.setCommentCount(map.get(tbUserCommentInitPo.getId()));
            tbUserCommentInitPoList.add(tbUserCommentInitPo1);
        }


        return tbUserCommentInitPoList;
    }

    @Override
    public List<TbUserCommentInitPo> tbUserCommentInitPos(TbUserCommentInitPo tbUserCommentInitPo) {
        return tbUserCommentInitMapper.tbUserCommentInitPos(tbUserCommentInitPo);
    }

    @Override
    public int statusAddLeven(TbUserCommentInit commentDto) {
        return tbUserCommentInitMapper.insertSelective(commentDto);
    }

}
